﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using System.Data;

namespace DuncanDAL
{
    public class EarningRecordsDataService : IDAL<T_EarningRecordsObject>
    {
        private const string SQL_INSERT_EarningRecords = "insert into T_EarningRecords("+
            " EarningItem,EarningObject,EarningSum,EarningDate,EarningCategory,EarningMemo) values("+
            " '{0}',{1},{2},'{3}',{4},'{5}')";

        private const string SQL_DELETE_EarningRecords = "delete from T_EarningRecords where OID={0}";

        private const string SQL_UPDATE_EarningRecords = "update T_EarningRecords set EarningItem='{0}',"+
            " EarningObject={1},EarningSum={2},EarningDate='{3}',EarningCategory={4},EarningMemo='{5}',"+
            " where OID={6}";

        private const string SQL_SELECT_EarningRecords = "select *  from T_EarningRecords ";

        private const string SQL_SELECT_SINGLE_EarningRecords = "select *  from T_EarningRecords  where OID={0}";

        #region IDAL<T_EarningRecordsObject> 成员

        public void Create(T_EarningRecordsObject obj)
        {
            string sql = string.Format(SQL_INSERT_EarningRecords, obj.Db_EarningItem, obj.Db_EarningObject, obj.Db_EarningSum, obj.Db_EarningDate, obj.Db_EarningCategory, obj.Db_EarningMemo);
            DBHelper.Excute(sql);
        }

        public void Delete(T_EarningRecordsObject obj)
        {
            string sql = string.Format(SQL_DELETE_EarningRecords, obj.Db_OID);
            DBHelper.Excute(sql);
        }

        public void Update(T_EarningRecordsObject obj)
        {
            string sql = string.Format(SQL_UPDATE_EarningRecords, obj.Db_EarningItem, obj.Db_EarningObject, obj.Db_EarningSum, obj.Db_EarningDate, obj.Db_EarningCategory, obj.Db_EarningMemo,obj.Db_OID);
            DBHelper.Excute(sql);
        }

        public IList<T_EarningRecordsObject> QueryAll()
        {
            IList<T_EarningRecordsObject> list = new List<T_EarningRecordsObject>();
            DataTable dtT_EarningRecords = new DataTable("T_EarningRecords");
            DBHelper.FillData(SQL_SELECT_EarningRecords, dtT_EarningRecords);
            foreach (DataRow item in dtT_EarningRecords.Rows)
            {
                T_EarningRecordsObject o = new T_EarningRecordsObject();
                o.Data2Object(item);
                list.Add(o);
            }
            return list;
        }

        public T_EarningRecordsObject Get(object id)
        {
            DataTable dtT_EarningRecords = new DataTable("T_EarningRecords");
            DBHelper.FillData(SQL_SELECT_SINGLE_EarningRecords, dtT_EarningRecords);
            T_EarningRecordsObject o = null;
            foreach (DataRow item in dtT_EarningRecords.Rows)
            {
                o = new T_EarningRecordsObject();
                o.Data2Object(item);
            }
            return o;
        }

        #endregion
    }
}
